#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

void handler(int num) {
//我接受到了SIGCHLD的信号啦
    int status;
    int pid = waitpid(-1, &status, WNOHANG);
    if (WIFEXITED(status)) {
        printf("The child %d exit with code %d\n", pid, WEXITSTATUS(status));
    }
}

int main() {
//子进程的pid
    int c_pid;
    int pid;

    signal(SIGCHLD, handler);


    pid_t p1 = fork();
    if( p1 == 0 )
    {
        printf("in child 1, pid = %d\n", getpid());
        //while(1);//进入子进程1的处理函数
        return 0; //若此处没有return 0 p1 进程也会执行 pid_t p2=fork()语句
    }

    pid_t p2 = fork();
    if( p2 == 0 ) {
        printf("in child 2, pid = %d\n", getpid());
        //while(1);//进入子进程2的处理函数
        return 0; //子进程结束，跳回父进程
        //printf("hello world\");//没有打印
    }
    while(1);
}